package com.hemerasolutions.v2trainer.component.search;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import com.hemerasolutions.v2trainer.component.BaseComponent;
import com.hemerasolutions.v2trainer.component.interfaces.detail.ExerciseDetailsCompInterface;
import com.hemerasolutions.v2trainer.component.interfaces.search.ExerciseSearchCompInterface;
import com.hemerasolutions.v2trainer.component.interfaces.search.ModalitySearchCompInterface;
import com.hemerasolutions.v2trainer.delegate.interfaces.ExerciseSearchDelegateInterface;
import com.hemerasolutions.v2trainer.delegate.interfaces.ModalitySearchDelegateInterface;
import com.hemerasolutions.v2trainer.model.exercise.Exercise;
import com.hemerasolutions.v2trainer.model.exercise.executions.Execution;
import com.hemerasolutions.v2trainer.model.modality.Modality;
import com.hemerasolutions.v2trainer.model.modality.VersionableModality;
import com.hemerasolutions.v2trainer.model.muscle.BodyRegion;
import com.hemerasolutions.v2trainer.model.muscle.Muscle;
import com.hemerasolutions.v2trainer.model.muscle.MuscleGroup;
import com.hemerasolutions.v2trainer.model.training.TrainingType;
import com.hemerasolutions.v2trainer.service.interfaces.ModalityServiceInterface;


@Component("exerciseSearchComp")
@Scope("prototype")
public class ExerciseSearchComp extends BaseComponent implements ExerciseSearchCompInterface, ModalitySearchDelegateInterface{

	private static final long serialVersionUID = 1L;

	private String findExerciseByName;
	
	private String findExerciseByDescription;
	
	private boolean findExerciseByTechnical;
	
	private List<TrainingType> findExerciseByTrainingTypes;
	
	private List<BodyRegion> findExerciseByBodyRegions;
	
	private List<MuscleGroup> findExerciseByMuscleGroups;
	
	private List<Execution> findExerciseByExecutions;
	
	private List<Muscle> findExerciseByMuscles;
	
	private List<Muscle> fullMuscles;
	
	private List<Execution> fullExecutions;
	
	private List<MuscleGroup> fullMuscleGroups;
	
	private List<BodyRegion> fullBodyRegions;
	
	private List<Exercise> foundExercises; 

	private Modality findExerciseByModality;
	
	private Exercise exercise;
	
	private Exercise selExercise;
	
	private ExerciseSearchDelegateInterface exerciseSearchDelegate;
	
	@Autowired
	private ModalityServiceInterface modalityService;

	@Autowired
	private ExerciseDetailsCompInterface exerciseDetailsHandler;
	
	@Autowired
	private ModalitySearchCompInterface modalitySearch;
	
	@Override
	public void refresh() {
		this.foundExercises = new ArrayList<Exercise>();
		this.fullExecutions = new ArrayList<Execution>();
		this.fullBodyRegions = this.modalityService.listBodyRegion();
		this.fullMuscleGroups = new ArrayList<MuscleGroup>();
		this.fullMuscles = new ArrayList<Muscle>();
	}
	
	@Override
	public void toViewExercise(){
		this.exerciseDetailsHandler.setExercise(this.exercise);
		this.exerciseDetailsHandler.refresh();
	}
	
	
	@Override
	public void toSelectModality(){
		this.modalitySearch.setDelegate(this);
		this.exerciseDetailsHandler.refresh();
	}
	

	@Override
	public void addExerciseStep(){
		if(this.exerciseSearchDelegate.onExerciseAdd(this.selExercise)){
			this.foundExercises.remove(this.selExercise);
		}
	}
	
	@Override
	public void searchExercises(){
    	Map<String,Object> fields = new HashMap<String, Object>();
    	
    	if (!this.findExerciseByName.equals("")) {
            fields.put("name", this.findExerciseByName);
        }

    	if (!this.findExerciseByDescription.equals("")) {
            fields.put("description", this.findExerciseByDescription);
        }

    	if( this.findExerciseByTrainingTypes != null && !this.findExerciseByTrainingTypes.isEmpty()){
    	    fields.put("trainingTypes", this.findExerciseByTrainingTypes);
    	}

    	if( this.findExerciseByModality != null ){
    	    fields.put("modality", this.findExerciseByModality);
    	}
    	
    	if( this.findExerciseByExecutions != null && !this.findExerciseByExecutions.isEmpty()){
    	    fields.put("executions", this.findExerciseByExecutions);
    	}

    	if( this.findExerciseByBodyRegions != null && !this.findExerciseByBodyRegions.isEmpty()){
    	    fields.put("bodyRegions", this.findExerciseByBodyRegions);
    	}

    	if( this.findExerciseByMuscleGroups != null && !this.findExerciseByMuscleGroups.isEmpty()){
    	    fields.put("muscleGroups", this.findExerciseByMuscleGroups);
    	}

    	if( this.findExerciseByMuscles != null && !this.findExerciseByMuscles.isEmpty()){
    	    fields.put("mucles", this.findExerciseByMuscles);
    	}
    	
    	fields.put("technical", this.findExerciseByTechnical);
    	
        fields.put("librarys", this.getActiveUserAllowedLibrarys());
        
		
	    this.foundExercises = this.modalityService.searchExercises(fields);

		if((this.foundExercises).isEmpty()){
			this.showInfoMessage(	this.localizedText("SearchCompleted"),
									this.localizedText("NoEntrysFounded"));
		}else{
			this.showInfoMessage(	this.localizedText("SearchCompleted"),
									this.foundExercises.size() + " " + 
									this.localizedText("entrysFounded"));
		}
	}	
		

	@Override
	public void changeFullBodyRegions(){
		this.fullMuscleGroups = new ArrayList<MuscleGroup>();
		for(BodyRegion br:this.findExerciseByBodyRegions){
			for(MuscleGroup mg:br.getMuscleGroups()){
				this.fullMuscleGroups.add(mg);
			}
		}
	}
	
	@Override
	public void changeFullMuscleGroups(){
		this.fullMuscles = new ArrayList<Muscle>();
		for(MuscleGroup mg:this.findExerciseByMuscleGroups){
			for(Muscle musc:mg.getMuscles()){
				this.fullMuscles.add(musc);
			}
		}
	}	

	@Override
	public String getFindExerciseByName() {
		return findExerciseByName;
	}

	@Override
	public void setFindExerciseByName(String findExerciseByName) {
		this.findExerciseByName = findExerciseByName;
	}

	@Override
	public String getFindExerciseByDescription() {
		return findExerciseByDescription;
	}

	@Override
	public void setFindExerciseByDescription(String findExerciseByDescription) {
		this.findExerciseByDescription = findExerciseByDescription;
	}

	@Override
	public boolean isFindExerciseByTechnical() {
		return findExerciseByTechnical;
	}

	@Override
	public void setFindExerciseByTechnical(boolean findExerciseByTechnical) {
		this.findExerciseByTechnical = findExerciseByTechnical;
	}

	@Override
	public List<TrainingType> getFindExerciseByTrainingTypes() {
		return findExerciseByTrainingTypes;
	}

	@Override
	public void setFindExerciseByTrainingTypes(
			List<TrainingType> findExerciseByTrainingTypes) {
		this.findExerciseByTrainingTypes = findExerciseByTrainingTypes;
	}

	@Override
	public List<BodyRegion> getFindExerciseByBodyRegions() {
		return findExerciseByBodyRegions;
	}

	@Override
	public void setFindExerciseByBodyRegions(
			List<BodyRegion> findExerciseByBodyRegions) {
		this.findExerciseByBodyRegions = findExerciseByBodyRegions;
	}

	@Override
	public List<MuscleGroup> getFindExerciseByMuscleGroups() {
		return findExerciseByMuscleGroups;
	}

	@Override
	public void setFindExerciseByMuscleGroups(
			List<MuscleGroup> findExerciseByMuscleGroups) {
		this.findExerciseByMuscleGroups = findExerciseByMuscleGroups;
	}

	@Override
	public List<Execution> getFindExerciseByExecutions() {
		return findExerciseByExecutions;
	}

	@Override
	public void setFindExerciseByExecutions(List<Execution> findExerciseByExecutions) {
		this.findExerciseByExecutions = findExerciseByExecutions;
	}

	@Override
	public List<Muscle> getFindExerciseByMuscles() {
		return findExerciseByMuscles;
	}

	@Override
	public void setFindExerciseByMuscles(List<Muscle> findExerciseByMuscles) {
		this.findExerciseByMuscles = findExerciseByMuscles;
	}

	@Override
	public List<Muscle> getFullMuscles() {
		return fullMuscles;
	}

	@Override
	public void setFullMuscles(List<Muscle> fullMuscles) {
		this.fullMuscles = fullMuscles;
	}

	@Override
	public List<Execution> getFullExecutions() {
		return fullExecutions;
	}

	@Override
	public void setFullExecutions(List<Execution> fullExecutions) {
		this.fullExecutions = fullExecutions;
	}

	@Override
	public List<MuscleGroup> getFullMuscleGroups() {
		return fullMuscleGroups;
	}

	@Override
	public void setFullMuscleGroups(List<MuscleGroup> fullMuscleGroups) {
		this.fullMuscleGroups = fullMuscleGroups;
	}

	@Override
	public List<BodyRegion> getFullBodyRegions() {
		return fullBodyRegions;
	}

	@Override
	public void setFullBodyRegions(List<BodyRegion> fullBodyRegions) {
		this.fullBodyRegions = fullBodyRegions;
	}

	@Override
	public List<Exercise> getFoundExercises() {
		return foundExercises;
	}

	@Override
	public void setFoundExercises(List<Exercise> foundExercises) {
		this.foundExercises = foundExercises;
	}



	@Override
	public Exercise getExercise() {
		return exercise;
	}



	@Override
	public void setExercise(Exercise exercise) {
		this.exercise = exercise;
	}


	@Override
	public ExerciseSearchDelegateInterface getExerciseSearchDelegate() {
		return exerciseSearchDelegate;
	}

	@Override
	public void setExerciseSearchDelegate(
			ExerciseSearchDelegateInterface exerciseSearchDelegate) {
		this.exerciseSearchDelegate = exerciseSearchDelegate;
	}

	@Override
	public Exercise getSelExercise() {
		return selExercise;
	}

	@Override
	public void setSelExercise(Exercise selExercise) {
		this.selExercise = selExercise;
	}

	@Override
	public ExerciseDetailsCompInterface getExerciseDetailsHandler() {
		return exerciseDetailsHandler;
	}

	@Override
	public void setExerciseDetailsHandler(
			ExerciseDetailsCompInterface exerciseDetailsHandler) {
		this.exerciseDetailsHandler = exerciseDetailsHandler;
	}


	@Override
	public ModalitySearchCompInterface getModalitySearch() {
		return modalitySearch;
	}

	@Override
	public void setModalitySearch(ModalitySearchCompInterface modalitySearch) {
		this.modalitySearch = modalitySearch;
	}

	@Override
	public boolean onVersionableModalitySelected(
			VersionableModality versionableModality) {
		this.findExerciseByModality = versionableModality.getModality();
		return false;
	}

	@Override
	public void onVersionableModalitySearchCompleted(
			List<VersionableModality> versionableModalitys) {
		// Nothing to do
		
	}


	
	
}
